package org.learn;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: qingle
 * @Date: 2024/9/8-7:27
 * @Description: ScheduledExecutorService
 * @version: 1.0
 */
public class ScheduledTest {

	// 获取对象
	public static void main(String[] args) {
		ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
		scheduledExecutorService.scheduleAtFixedRate(
//		scheduledExecutorService.scheduleWithFixedDelay(
			() ->
			{
				System.out.println(System.currentTimeMillis());
				try
				{
					TimeUnit.SECONDS.sleep(6);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}, 2, 3,
			TimeUnit.SECONDS);
	}
	public static void schedule() {
		ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
		// 延时2s之后执行任务
		ScheduledFuture<?> schedule = service.schedule(() -> System.out.println(Thread.currentThread().getName()), 2, TimeUnit.SECONDS);

		service.shutdown();
	}
}
